package com.example.tellwin;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.common.Common;
import com.example.tellwin.view.ZoomImageView;

import java.util.ArrayList;

public class PhotoActivity extends CpBaseActivty {

    @BindView(R.id.vp2)
    ViewPager2 viewPager2;

    ArrayList<String> photoPaths = new ArrayList<>();
    ViewPagerAdapter viewPagerAdapter;
    int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        viewPagerAdapter = new ViewPagerAdapter();
        viewPager2.setAdapter(viewPagerAdapter);
        viewPager2.setUserInputEnabled(false);
    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void initDatas() {
        photoPaths = getIntent().getStringArrayListExtra(Common.PHOTO_PATH);
        position = getIntent().getIntExtra(Common.POSITION , 0);
        viewPagerAdapter.notifyDataSetChanged();
        viewPager2.setCurrentItem(position);
    }


    class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(PhotoActivity.this).inflate(R.layout.item_photo_vp, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Glide.with(PhotoActivity.this).load(photoPaths.get(position)).into(holder.zoomImageView);
        }

        @Override
        public int getItemCount() {
            return photoPaths.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            ZoomImageView zoomImageView;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                zoomImageView = itemView.findViewById(R.id.item_zoom_iv);
            }
        }
    }
}